package com.guaju.yueshangLife.fragment.goods;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.adapter.CommentsAdapter;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.base.BaseFragment;
import com.guaju.yueshangLife.bean.CommentsBean;
import com.guaju.yueshangLife.databinding.FragmentCommentBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.ToastUtil;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class CommentFragment extends Fragment {
    public static final String PRODUCT_ID = "product_id";
    private String product_id;
    private int filter = 0;
    private int page = 1;
    private int limit = 10;
    private FragmentCommentBinding binding;
    private LinearLayoutManager linearLayoutManager;


    public CommentFragment() {
    }


    private static CommentFragment fragment = null;

    public static CommentFragment newInstance(String product_id) {
        fragment = new CommentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("product_id", product_id);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_comment, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        linearLayoutManager = new LinearLayoutManager(getActivity()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvComment.setLayoutManager(linearLayoutManager);


        if (arguments != null) {
            product_id = arguments.getString(PRODUCT_ID);
            ApiEntity.comments(product_id, filter, page, limit, new Callback<CommentsBean>() {

                @Override
                public void onResponse(Call<CommentsBean> call, Response<CommentsBean> response) {
                    if (!response.isSuccessful() || response.body() == null) {
                        showEmpty();
                    } else {
                        CommentsBean body = response.body();
                        List<CommentsBean.DataBean> data = body.getData();
                        if (data == null || data.isEmpty()) {
                            binding.sortbar.setVisibility(View.GONE);
                            binding.noComment.setVisibility(View.VISIBLE);
                            showEmpty();
                        } else {
                            binding.noComment.setVisibility(View.GONE);
                            updateView(data);
                        }
                    }
                }

                @Override
                public void onFailure(Call<CommentsBean> call, Throwable t) {
                    showEmpty();
                }
            });
        }
    }

    private void updateView(List<CommentsBean.DataBean> data) {

        CommentsAdapter commentsAdapter = new CommentsAdapter(getActivity(), data);
        binding.rvComment.setAdapter(commentsAdapter);
        if (data.size() < 10) {
            commentsAdapter.updateLoadStatus(CommentsAdapter.LOAD_END);
        }


    }

    private void showEmpty() {
//        ToastUtil.show("暂无订单信息！");
    }
}
